package top.revery.task.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import top.revery.api.api.user.ICompanyApi;
import top.revery.api.config.UserPageData;
import top.revery.api.entity.user.BaseCompanyUser;
import top.revery.api.entity.user.BaseUserKol;
import top.revery.task.dto.CompanyDto;
import top.revery.task.service.CompanyService;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author denghao
 * @title
 * @package 企业
 * @date 2019/12/13 16:50
 */
@RestController
//@Transactional
public class CompanyController implements ICompanyApi {
    @Autowired
    private CompanyService companyService;
    @Autowired
    private CompanyDto companyDto;

    //保存企业信息
    @Override
    public BaseCompanyUser save(BaseCompanyUser baseCompanyUser) {

        try {
            return companyDto.save(baseCompanyUser);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //修改企业信息
    @Override
    public void update(BaseCompanyUser baseCompanyUser, Long id) throws Exception{

            baseCompanyUser.setId(id);
            String tele = baseCompanyUser.getTelephone();
            String email = baseCompanyUser.getEmail();
            if (!tele.matches("^1[^124]\\d{9}$") || !email.matches("^[A-Za-z0-9]+([_\\.][A-Za-z0-9]+)*@([A-Za-z0-9\\-]+\\.)+[A-Za-z]{2,6}$")) {
                throw new Exception("格式不对！请重新输入！");
            }
            Optional<BaseCompanyUser> byId = companyDto.findById(id);
            if (!byId.get().getTelephone().equals(baseCompanyUser.getTelephone())) {
                BaseUserKol baseTel = companyDto.findByTelephone(baseCompanyUser.getTelephone());
                if (baseTel != null) {
                    throw new Exception("输入号码有误！请重新输入！");
                }
            }
            if (!byId.get().getEmail().equals(baseCompanyUser.getEmail())) {
                BaseUserKol baseEamil = companyDto.findByEmail(baseCompanyUser.getEmail());
                if (baseEamil != null) {
                    throw new Exception("输入邮箱有误！请重新输入！");
                }
            }
            companyDto.save(baseCompanyUser);

    }

    //分页多条件查询所有
    @Override
    public UserPageData getList(int pageNum, int pageSize, Map map) {
        Page<BaseCompanyUser> pageList = companyService.findPage(pageNum, pageSize, map);
        UserPageData userPageData = new UserPageData();
        userPageData.list = pageList.getContent();
        userPageData.rows = pageList.getTotalElements();
        return userPageData;

    }

    @Override
    public BaseCompanyUser findById(Long id) {
        return companyDto.findById(id).get();
    }

    //根据企业名模糊查询企业
    @Override
    public List<BaseCompanyUser>  findByName(String name) {
        try {

            List<BaseCompanyUser>  list = companyDto.findByNameLike(name);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
